<?php
require_once "config.php";
require_once __DIR__ . '/qiniu/autoload.php';
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

if($_POST['insert'] == 1){
    $query="INSERT INTO dyspw_weblink (siteid, classid, parentstr, webname, webnote, picurl, linkurl, hits, zan, orderid, posttime, checkinfo, nickname, avatar, openid, u_id, lat, lng, goods_id, shop_id, report) VALUES ('$_POST[siteid]','$_POST[calssid]','0.','$_POST[webname]','$_POST[webnote]','$_POST[picurl]','$_POST[linkurl]','0','0','0',".time().",'false','','$_POST[avatar]','','$_POST[u_id]','','','0','0','0')";
    $success = $mysql->query($query);
    if($success){
        $back_info=['code'=>200,'msg'=>'入库成功'];
        echo json_encode($back_info);die;
    }else{
        $back_info=['code'=>400,'msg'=>'入库失败'];
        echo json_encode($back_info);die;
    }

}else{

$file = $_FILES;
$now_time = date('Y-m-d'); // make sure your loacl env, check your timezone setting, maybe in php.ini
// <b>Warning</b>:  date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in <b>E:\myphp_www\PHPTutorial\WWW\git_xcx\fileup.php</b> on line <b>9</b><br />
// above is your error message

function upload_file($file,$now_time ,$imagesExt=['avi','mpeg','quicktime','mp4','wmv','mov','MOV','rmvb','flv','3gp'])

{
    $accessKey = 'dZ4JiqyNgc6WgVEixLPUSzrEr3jUloZewEuV4gJI';
    $secretKey = 'Intd7MmakuzmxeyY-OeEUPZHWFVaezLArY8Fid_9';
    $auth = new Auth($accessKey, $secretKey);
    // 要上传的空间
    $bucket = 'education5p8';

    // 生成上传 Token
    $token = $auth->uploadToken($bucket);

    // 要上传文件的本地路径
    $filePath = "{$file['file']['tmp_name']}";
    // print_r($file['file']) ;exit;
    //截取文件的后缀
    $arr=explode('.', $file['file']['name']);
    $suffix = end($arr);

    // 上传到七牛后保存的文件名
    $key = time()."-"."{$now_time}".".".$suffix;

    // 判断错误号
    if (@$file['file']['error'] == 00) {
        // 判断文件类型
        $ext = strtolower(pathinfo(@$file['file']['name'],PATHINFO_EXTENSION));
        if (!in_array($ext,$imagesExt)){
            $back_info=['code'=>0,'msg'=>'非法格式'];
           echo json_encode($back_info);die;

        }

// 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();

        // 调用 UploadManager 的 putFile 方法进行文件的上传。
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        // echo "\n====> putFile result: \n";
        if ($err !== null) {
            print_r($err);
        } else {
            $path = "http://jianshen.fyh5p8.com/".$ret['key'];
        	$back_info=['code'=>1,'msg'=>$path];

        	echo json_encode($back_info);die;

        }


    } else {

        // 根据错误号返回提示信息

        switch (@$files['error']) {

            case 1:

                echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值";

                break;

            case 2:

                echo "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值";

                break;

            case 3:

                echo "文件只有部分被上传";

                break;

            case 4:

                echo "没有文件被上传";

                break;

            case 6:

            case 7:

                echo "系统错误";

                break;

        }

    }

}

echo upload_file($file,$now_time);
}
?>
